﻿using System;
using System.Collections.Generic;
using System.Linq;
using CommonTestingUtils;
using LightPersist;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace LightPersistTests
{
	[TestClass]
	public class Core_structs_should_behave_like_usual : FixtureMsTest
	{
		static readonly Random _rnd = new Random();
		readonly List<BackReference> _listBackref = new List<BackReference>();
		readonly List<Reference> _listRef = new List<Reference>();
		readonly List<string> _listSystem = new List<string>();

		[TestMethod]
		public void Should_remove_one_by_one_using_struct_equality()
		{
			Test(_listSystem, () => _rnd.Next(100).ToString(), x => (string)x.Clone());
			Test(_listBackref, () => new BackReference(Guid.NewGuid(), _rnd.Next(100).ToString()), x => new BackReference(x.ID, x.PropertyName));
			Test(_listRef, () => new Reference(Guid.NewGuid(), typeof(string)), x => new Reference(x.ID, x.TypeOfProperty));
		}

		void Test<T>(ICollection<T> list, Func<T> fact, Func<T, T> clone)
		{
			var item1 = fact();
			list.Add(item1);
			list.Add(item1);
			list.Add(fact());
			Equal(3, list.Count);
			var clone1 = clone(item1);
			list.Remove(clone1);
			Equal(2, list.Count);
			list.Remove(clone1);
			Equal(1, list.Count);
		}

	}
}